package com.wangyk.mybatis.main;

import com.wangyk.mybatis.pojo.MybatisUser;
import com.wangyk.mybatis.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBUtilMain
{
    public static final String SELECT_ALL_USER_SQL = "SELECT * FROM USER WHERE USENAME like ?";
    
    public static void main(String[] args)
        throws SQLException, ClassNotFoundException
    {
        Connection con = DBUtil.getCon();
        PreparedStatement pst = con.prepareStatement(SELECT_ALL_USER_SQL);
        pst.setString(1, "%a%");
        ResultSet rs = pst.executeQuery();
        List<MybatisUser> users = new ArrayList<MybatisUser>();
        MybatisUser user = null;
        while (rs.next())
        {
            user = new MybatisUser();
            user.setUseid(rs.getInt("useid"));
            user.setUsename(rs.getString("usename"));
            user.setPasswords(rs.getString("passwords"));
            user.setDate(rs.getDate("date"));
            users.add(user);
        }
        // 遍历集合中的所有元素
        for (MybatisUser tempUser : users)
        {
            System.out.println(tempUser);
        }
        DBUtil.Close(con, pst, rs);
    }
}
